﻿using System;
using System.Linq;
using Enyim.Caching;
using Ayatta.Contract;
using Newtonsoft.Json;
using System.Web.Mvc;
using Web.Item.Models;
using Ayatta.Contract.Extension;
using Web.Core.Models;
namespace Web.Item.Controllers
{
    public class HomeController : BaseController
    {
        public HomeController(Lazy<IInfrastructureService> infrastructure, Lazy<IPassportService> passport, Lazy<IAccountService> account, Lazy<IStoreService> store, Lazy<ITradeService> trade, Lazy<IPlayService> play, Lazy<IMiscService> misc, Lazy<IExtraService> extra, IMemcachedClient memcachedClient)
            : base(infrastructure, passport, account, store, trade, play, misc, extra, memcachedClient)
        {
        }

        public ActionResult Index(int param = 0)
        {

            var model = new HomeControllerModel.Index();
            var item = Store.Value.ProductItemGet(param);
            model.Item = item;
            if (item.ProductItemSkus != null && item.ProductItemSkus.Count > 0)
            {
                var skus = item.ProductItemSkus.ToDictionary(o => o.PropId, o => new { id = o.Id, itemId = o.ItemId, price = o.Price, count = o.Quantity });
                model.SkusJson = JsonConvert.SerializeObject(skus);
                model.SalePropCount = item.ProductItemSkus.SaleProps().Count;
            }
            else
            {
                model.SkusJson = "{}";
            }
            return View(model);
        }
    }
}
